﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTreeLib;
using LiveSequence.Common.Domain;
using LiveSequence.Common.Tree;
using LiveSequence.Suite.Documents;


namespace LiveSequence.Suite.Dialogs
{
  public partial class DlgMethodSelector : Form
  {
    private AssemblyExplorer _assemblyExplorer;
    private DTreeNode<DTreeItem> _root;
    private TreeViewController<DTreeItem> _tvController;

    public DlgMethodSelector()
    {
      InitializeComponent();

      _assemblyExplorer = AssemblyExplorer.Instance;
    }

    public MethodData MethodData
    {
      get; set;
    }

    private void DlgMethodSelector_Load(object sender, EventArgs e)
    {
      _root = _assemblyExplorer.AssemblyParser.AssemblyData.AssemblyTree;
      _tvController = new TreeViewController<DTreeItem>(_treeView, _root);
    }

    private void _treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
      DTreeNode<DTreeItem> node = _tvController.GetDataNode(e.Node);
      _btnSelect.Enabled = (node.Value.SelectionType == SelectionType.METHOD);
    }

    private void _treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
      DTreeNode<DTreeItem> node = _tvController.GetDataNode(e.Node);
      if (node.Value.SelectionType == SelectionType.METHOD)
      {
        SelectedNode(node);
      }
    }

    private void _btnSelect_Click(object sender, EventArgs e)
    {
      DTreeNode<DTreeItem> node = _tvController.GetDataNode(_treeView.SelectedNode);
      SelectedNode(node);
    }

    private void SelectedNode(DTreeNode<DTreeItem> node)
    {
      string strMethodName = node.Value.Text;
      string strTypeName = node.Parent.Value.Text;
      string strNamespace = node.Parent.Parent.Value.Text;
      string strAssemblyName = node.Parent.Parent.Parent.Value.Text;
      
      MethodData = new MethodData(strAssemblyName, strNamespace, strTypeName, strMethodName);
      base.DialogResult = DialogResult.OK;
      Close();
    }


  }
}
